package com.example.demo.controller;

import com.example.demo.pojo.Goods;
import com.example.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    //商品详情查询
    @GetMapping("/findById")
    public Object findById(@RequestParam("id") Integer id) {
        HashMap<String, Object> map = new HashMap();
        Goods goods = goodsService.findById(id);
        if (goods != null) {
            map.put("code", 200);
            map.put("msg", "商品查询成功");
            map.put("data", goods);
        } else {
            map.put("code", 300);
            map.put("msg", "商品查询失败");
        }
        return map;
    }

    //商品列表查询
    @GetMapping("/query")
    public Object query() {
        HashMap<String, Object> map = new HashMap();
        List<Goods> list = goodsService.query();
        if (list.size() > 0) {
            map.put("code", 200);
            map.put("msg", "商品上架成功");
            map.put("data", list);
        } else {
            map.put("code", 300);
            map.put("msg", "暂无商品");
        }
        return map;
    }
}
